import 'package:flutter/material.dart';

import 'package:flutter_demo/widgets/groupSlotWidget.dart';
import 'package:flutter_demo/widgets/groupHeaderWidget.dart';
import 'package:flutter_demo/widgets/textWidget.dart';

import 'package:flutter_demo/models/activityItem.dart';

class MActivity extends StatelessWidget {
  final title;
  final List<ActivityItem> items;

  MActivity({
    this.title = "活动推荐",
    @required this.items,
  }) : assert(items != null);

  @override
  Widget build(BuildContext context) {
    double _screenWidth = MediaQuery.of(context).size.width;
    double _itemHeight = 200.0;

    final double _padding = 10.0;

    return MGroupSlot(
      padding: 0.0,
      header: MGroupHeader(title),
      body: Container(
        width: _screenWidth,
        height: _itemHeight,
        child: ListView.builder(
          padding: EdgeInsets.only(left: _padding),
          itemCount: items.length,
          itemBuilder: (BuildContext context, int index) {
            return Padding(
              padding: EdgeInsets.only(right: _padding),
              child: MActivityWidget(items[index], _screenWidth * 0.7),
            );
          },
          shrinkWrap: true,
          primary: false,
          physics: ClampingScrollPhysics(),
          scrollDirection: Axis.horizontal,
        ),
      ),
    );
  }
}

class MActivityWidget extends StatelessWidget {
  final ActivityItem item;
  final double imageWidth;

  MActivityWidget(
    this.item,
    this.imageWidth,
  );

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Image.network(
          item.imageSrc,
          width: imageWidth,
          height: imageWidth / 2,
          fit: BoxFit.cover,
        ),
        SizedBox(height: 8.0),
        MText(item.title),
        MText(
          item.detail,
          fontSize: 12.0,
          color: Colors.grey,
        ),
      ],
    );
  }
}
